package db

import (
	"bytes"
	"ginweb/module/db/model"

	// mysql driver
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"xorm.io/core"
)

var (

	// Ins is Database instance
	Ins *xorm.Engine
)

// Init Database
func Init(mysqlConnection string) error {
	db, err := xorm.NewEngine("mysql", mysqlConnection)
	if err != nil {
		return err
	}

	Ins = db

	// 显示sql执行，便于调试
	Ins.SetTableMapper(core.SnakeMapper{})

	// 不存在用户表则直接初始化数据库
	isext, err := Ins.IsTableExist(model.SysUser{})
	if err == nil && !isext {
		_, err := Ins.Import(bytes.NewBufferString(sqls))
		if err != nil {
			println("database init err:" + err.Error())
		}
	}
	return nil
}
